# -*- coding: utf-8 -*-
# @Time    : 2020/10/22 14:54
# @Author  : Leo
# @FileName: job_process.py
# @Software: PyCharm


import time
from utils.time_utils import dateTime_to_timeStamp, compare_orderTime
from apscheduler.schedulers.blocking import BlockingScheduler
from modules.user.wechat_request import WechatRequest
from modules.user.user_manage import UserManager
from models.user.user_model import UserModel

def alert_user_to_order():
    '''
    提醒用户订餐
    @return:
    '''
    user_list = UserManager().list()
    for user in user_list:
        # 最近一次订餐时间
        order_time = dateTime_to_timeStamp(user['orderTime'])
        # 用户最近一次订餐时间在7天内 且还没有订餐
        if compare_orderTime(order_time) and  user['isOrder']==0 and user['weChartID']!="":
            WechatRequest.sendMessage(openid=user['weChartID'], content='马上订餐时间截止,如您需要用餐请及时订餐！')

def alert_user_to_eat():
    '''
    提醒用户用餐（已经订餐）
    @return: 
    '''
    user_list = UserManager().list()
    for user in user_list:
        # 用户最近一次订餐时间在7天内 且还没有订餐
        if  user['isOrder'] == 1 and user['weChartID']!="":
            WechatRequest.sendMessage(openid=user['weChartID'], content='用餐时间到了,请及时用餐！')


def reset_all_user():
    '''
    每天定时重置用户的订单状态
    @return:
    '''
    user_list = UserManager().list()
    for user in user_list:
        if user['isOrder'] == 1:
            UserModel().update_orderDataByName(name=user['name'], orderTime=user['orderTime'])


# def start_job():
scheduler = BlockingScheduler()

# # 每天的16点半提醒订餐
# scheduler.add_job(alert_user_to_order, 'cron', day_of_week='0-7', hour='16', minute='30', second='0')
#
# # 每个周末的10点半提醒订餐
# scheduler.add_job(alert_user_to_order, 'cron', day_of_week='5-7', hour='10', minute='30', second='0')
#
# # 每天的18点45提醒用餐
# scheduler.add_job(alert_user_to_eat, 'cron', day_of_week='0-7', hour='18', minute='45', second='0')
# # 每个周末的12点提醒用餐
# scheduler.add_job(alert_user_to_eat, 'cron', day_of_week='5-7', hour='12', minute='0', second='0')
#
# # 每天19点半重置用户的订餐状态
# scheduler.add_job(alert_user_to_order, 'cron', day_of_week='0-7', hour='19', minute='30', second='0')


# 演示用
# 重置订单状态
# reset_all_user()
# 每隔10s执行一次提醒订餐
scheduler.add_job(alert_user_to_order, 'interval', seconds=10)
# 每隔15s执行一次提醒用餐
scheduler.add_job(alert_user_to_eat, 'interval', seconds=15)

scheduler.start()